package id.co.rho.scoring.cid.client.mvc.versioning;

import id.co.rho.scoring.cid.client.command.CommandAction;
import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelCommand;
import id.co.rho.scoring.cid.client.command.impl.ScoringModelResult;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigResult;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand.CommandType;
import id.co.rho.scoring.cid.client.command.impl.VersioningSetDefaultCommandAction;
import id.co.rho.scoring.cid.client.command.impl.VersioningSetDefaultCommandResult;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class VersioningSetDefaultView extends View{
	private Credential credential;
	private CommandDispatcherAsync dispatcher;
	private VersioningSetDefaultUI ui;
	private Long modelId;
	
	@Inject
	public VersioningSetDefaultView(CommandDispatcherAsync dispacther, Credential credential,
			VersioningSetDefaultUI ui) {
		super(null);
		this.dispatcher = dispacther;
		this.credential = credential;
		this.ui = ui;
		controller = new ViewController();
	}
	
	@Override
	public void initialize(){
		super.initialize();
		ui.initialize();
		ui.getBtnSetDefault().addSelectionListener(
				new SelectionListener<ButtonEvent>() {
					@Override
					public void componentSelected(ButtonEvent ce) {
						setAsDefault();
					}
				});
		ui.getTree().expandAll();
		
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if(event.getType().equals(AppEvents.VersionSetDefaultEvent)){
			ui.show();
			this.modelId = Long.valueOf(event.getData("modelId").toString());
			ScoringModelCommand cmd = new ScoringModelCommand();
			cmd.setModelId(this.modelId);
			dispatcher.execute(cmd, new AsyncCallback<ScoringModelResult>() {

				@Override
				public void onFailure(Throwable arg0) {
					
				}

				@Override
				public void onSuccess(ScoringModelResult arg0) {
					ui.setModel(arg0.folder);
				}
			});
		}
	}
	
	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.VersionSetDefaultEvent);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(VersioningSetDefaultView.this, event);
		}
	}

	private void setAsDefault() {
		VersioningSetDefaultCommandAction commandAction = new VersioningSetDefaultCommandAction();
		commandAction.setId(this.modelId);
		dispatcher.execute(commandAction, new AsyncCallback<VersioningSetDefaultCommandResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(VersioningSetDefaultCommandResult result) {
					Info.display("Saved", result.getOutput());
					AppEvent appEvent = new AppEvent(
							AppEvents.VersionModelEvent);
					Dispatcher.forwardEvent(appEvent);
				
			}

		});
		
	}
	
}
